import 'package:json_annotation/json_annotation.dart';

/**
 * 文件夹数据模型
 * 包含文件夹的基本信息和层级关系
 */
@JsonSerializable()
class Folder {
  final int id;
  final String name;
  final int count; // 笔记数量
  final bool isDefault; // 是否为默认文件夹
  final int? parentId; // 父文件夹ID
  final DateTime? createdAt;
  final DateTime? updatedAt;
  final bool? isShared; // 是否为共享文件夹
  final String? shareLink; // 共享链接
  final String? syncStatus; // 同步状态

  const Folder({
    required this.id,
    required this.name,
    this.count = 0,
    this.isDefault = false,
    this.parentId,
    this.createdAt,
    this.updatedAt,
    this.isShared = false,
    this.shareLink,
    this.syncStatus,
  });

  /**
   * 从 JSON 创建 Folder 对象
   */
  factory Folder.fromJson(Map<String, dynamic> json) {
    return Folder(
      id: json['id'] as int,
      name: json['name'] as String,
      count: json['count'] as int? ?? 0,
    );
  }

  /**
   * 将 Folder 对象转换为 JSON
   */
  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'name': name,
      'count': count,
    };
  }

  /**
   * 创建 Folder 的副本，可选择性地更新某些字段
   */
  Folder copyWith({
    int? id,
    String? name,
    int? count,
    bool? isDefault,
    int? parentId,
    DateTime? createdAt,
    DateTime? updatedAt,
    bool? isShared,
    String? shareLink,
    String? syncStatus,
  }) {
    return Folder(
      id: id ?? this.id,
      name: name ?? this.name,
      count: count ?? this.count,
      isDefault: isDefault ?? this.isDefault,
      parentId: parentId ?? this.parentId,
      createdAt: createdAt ?? this.createdAt,
      updatedAt: updatedAt ?? this.updatedAt,
      isShared: isShared ?? this.isShared,
      shareLink: shareLink ?? this.shareLink,
      syncStatus: syncStatus ?? this.syncStatus,
    );
  }

  /**
   * 判断是否为根文件夹
   */
  bool get isRoot => parentId == null;

  /**
   * 判断是否可以删除（非默认文件夹）
   */
  bool get canDelete => !isDefault;

  /**
   * 判断是否可以重命名（非默认文件夹）
   */
  bool get canRename => !isDefault;

  @override
  bool operator ==(Object other) =>
      identical(this, other) ||
      other is Folder &&
          runtimeType == other.runtimeType &&
          id == other.id;

  @override
  int get hashCode => id.hashCode;

  @override
  String toString() {
    return 'Folder{id: $id, name: $name, count: $count, isDefault: $isDefault}';
  }

  /**
   * 创建默认的"全部笔记"文件夹
   */
  static Folder get allNotes => const Folder(
        id: 1,
        name: '全部笔记',
        isDefault: true,
      );

  /**
   * 创建默认的"未分类笔记"文件夹
   */
  static Folder get uncategorized => const Folder(
        id: 2,
        name: '未分类笔记',
        isDefault: true,
      );
}